package com.haohan.jingyuan.gateway.aspect;

import com.haohan.jingyuan.common.enums.ErrorEnum;
import com.haohan.jingyuan.common.exception.BusinessException;
import com.haohan.jingyuan.gateway.holder.UserContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * NeedLoginAspect
 */
@Slf4j
@Aspect
@Component
public class NeedLoginAspect {

    @Around("@annotation(needLogin)")
    public Object injectHeaders(ProceedingJoinPoint joinPoint, NeedLogin needLogin) throws Throwable {


        String currentToken = UserContextHolder.getCurrentToken();
        String currentUserId = UserContextHolder.getCurrentUserId();
        String currentUserName = UserContextHolder.getCurrentUserName();

        if (StringUtils.isEmpty(currentToken)
                || StringUtils.isEmpty(currentUserId)
                || StringUtils.isEmpty(currentUserName)) {
            log.debug("没有登录信息");
            throw new BusinessException(ErrorEnum.USER_TLOGIN_ERROR);
        }

        return joinPoint.proceed();
    }
}